package ltd.trackan.commonredis.model.entity;

import lombok.AllArgsConstructor;
import lombok.Getter;
import ltd.trackan.commonredis.service.DistributedLock;


import java.util.Objects;

/**
 * RedissonLock 包装的锁对象 实现AutoCloseable接口，在java7的try(with resource)语法，不用显示调用close方法
 */
@AllArgsConstructor
public class ILock implements AutoCloseable {
    /**
     * 持有的锁对象
     */
    @Getter
    private Object lock;
    /**
     * 分布式锁接口
     */
    @Getter
    private DistributedLock distributedLock;

    @Override
    public void close() {
        if(Objects.nonNull(lock)){
            distributedLock.unLock(lock);
        }
    }
}
